#include "drv_timer.h"

	
void timer8_oc_int(void)
{
	tmr_output_config_type tmr_output_struct;

//	uint16_t timer_period = 0;

	crm_periph_clock_enable(CRM_TMR8_PERIPH_CLOCK, TRUE);

	/* compute the value to be set in arr regiter to generate signal frequency at 17.57 khz */
//	timer_period = (192000000 / 4000 ) - 1;


	/* compute ccr3 value to generate a duty cycle at 25%  for channel 3 and 3n */
//	channel3_pulse = (uint16_t)(((uint32_t) 25 * (timer_period - 1)) / 100);

	tmr_base_init(TMR8, TIMER8_PWM_CYCLE, 0);
	tmr_cnt_dir_set(TMR8, TMR_COUNT_UP);

	/* channel 1, 2, 3 and 4 configuration in output mode */
	tmr_output_default_para_init(&tmr_output_struct);
	tmr_output_struct.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_B;
	tmr_output_struct.oc_output_state = TRUE;
	tmr_output_struct.oc_polarity = TMR_OUTPUT_ACTIVE_LOW;
	tmr_output_struct.oc_idle_state = TRUE;
	tmr_output_struct.occ_output_state = TRUE;
	tmr_output_struct.occ_polarity = TMR_OUTPUT_ACTIVE_HIGH;
	tmr_output_struct.occ_idle_state = FALSE;

	/* channel 3 */
	tmr_output_channel_config(TMR8, TMR_SELECT_CHANNEL_3, &tmr_output_struct);
	tmr_channel_value_set(TMR8, TMR_SELECT_CHANNEL_3, 0);
	tmr_output_channel_config(TMR8, TMR_SELECT_CHANNEL_4, &tmr_output_struct);
	tmr_channel_value_set(TMR8, TMR_SELECT_CHANNEL_4, 0);

	/* output enable */
	tmr_output_enable(TMR8, TRUE);

	/* enable TMR8 */
	tmr_counter_enable(TMR8, TRUE);
}

void set_timer8_oc_val(u32 val, u8 c)
{
	if(val > TIMER8_PWM_CYCLE)
		val = TIMER8_PWM_CYCLE;
	
	if(c == 3)
		tmr_channel_value_set(TMR8, TMR_SELECT_CHANNEL_3, val);
	else if(c == 4)
		tmr_channel_value_set(TMR8, TMR_SELECT_CHANNEL_4, val);
}



void timer10_base_init(void)
{
	crm_periph_clock_enable(CRM_TMR10_PERIPH_CLOCK, TRUE);

	/* 主时钟200Mhz */
	tmr_base_init(TMR10, 100 - 1, 200 - 1);
	tmr_cnt_dir_set(TMR10, TMR_COUNT_UP);

	/* overflow interrupt enable */
	tmr_interrupt_enable(TMR10, TMR_OVF_INT, TRUE);

	/* tmr1 overflow interrupt nvic init */
	nvic_irq_enable(TMR1_OVF_TMR10_IRQn, 1, 1);

	/* enable tmr1 */
	tmr_counter_enable(TMR10, TRUE);
}


	
void timer3_oc_int(void)
{
	tmr_output_config_type tmr_output_struct;

	crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, TRUE);

	tmr_base_init(TMR3, TIMER3_PWM_CYCLE, 0);
	tmr_cnt_dir_set(TMR3, TMR_COUNT_UP);

	/* channel 1, 2, 3 and 4 configuration in output mode */
	tmr_output_default_para_init(&tmr_output_struct);
	tmr_output_struct.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_B;
	tmr_output_struct.oc_output_state = TRUE;
	tmr_output_struct.oc_polarity = TMR_OUTPUT_ACTIVE_LOW;
	tmr_output_struct.oc_idle_state = TRUE;
	tmr_output_struct.occ_output_state = TRUE;
	tmr_output_struct.occ_polarity = TMR_OUTPUT_ACTIVE_HIGH;
	tmr_output_struct.occ_idle_state = FALSE;

	/* channel 4 */
	tmr_output_channel_config(TMR3, TMR_SELECT_CHANNEL_3, &tmr_output_struct);
	tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_3, 0);

	/* output enable */
	tmr_output_enable(TMR3, TRUE);

	/* enable TMR3 */
	tmr_counter_enable(TMR3, TRUE);
}

void set_timer3_oc_val(u32 val, u8 c)
{
	if(val > TIMER3_PWM_CYCLE)
		val = TIMER3_PWM_CYCLE;
	
	if(c == 3)
		tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_3, val);
	else if(c == 4)
		tmr_channel_value_set(TMR3, TMR_SELECT_CHANNEL_4, val);
}



extern USER_PARA sys_user_para;


/**
 * @description:        TIM1 dma 传输控制
 * @param {uint16_t} s  ON开启传输，OFF停止传输
 * @return {*}          null
 */
void timer1_dma_transfer_ctrl(uint16_t s)
{
    if(s == ON)
    {
		dma_channel_enable(DMA1_CHANNEL1, TRUE);
 //       TIM_GenerateEvent(TIM3, TIM_EVT_SRC_UPDATE);
		tmr_event_sw_trigger(TMR1, TMR_OVERFLOW_SWTRIG);
		tmr_counter_enable(TMR1, TRUE);
    }
    else
    {
		dma_channel_enable(DMA1_CHANNEL1, FALSE);
		tmr_counter_enable(TMR1, FALSE);
    }
}

void change_color_to_bit(uint32_t rgb_color,uint16_t* bit)
{
	uint32_t temp = rgb_color;
	int i = 0;
	uint16_t temp_bit = 0;
	
    /**
     * rgb_color 颜色格式顺序为RGB
     * bit[23 - 16] = B0 - B7
     * bit[15 - 8 ] = G0 - G7
     * bit[7  - 0 ] = R0 - R7
     * 目前bit中的数据格式为RGB
     */
    for(i=0;i<24;i++){
		if(temp & 0x00000001){
			bit[23-i] = T1H_COUNT;
		}else{
			bit[23-i] = T0H_COUNT ;
		}
		temp = temp>>1;
	}

    /* RGB格式参数，调整字节顺序 */
    if(sys_user_para.cfg_para.rgb_format == RGB_FORMAT_RBG)
    {
        /* RGB >> RBG */
	    for(i=0;i<8;i++){
		    temp_bit = bit[8+i];	bit[8+i] = bit[16+i];	bit[16+i] = temp_bit;
	    }
    }
    else if(sys_user_para.cfg_para.rgb_format == RGB_FORMAT_GRB)
    {
        /* RGB >> GRB */
	    for(i=0;i<8;i++){
		    temp_bit = bit[i];	bit[i] = bit[8+i];	bit[8+i] = temp_bit;
	    }
    }
    else if(sys_user_para.cfg_para.rgb_format == RGB_FORMAT_GBR)
    {
        /* RGB >> GRB,  GRB >> GBR */
	    for(i=0;i<8;i++){
		     temp_bit = bit[i];   bit[i] = bit[8+i];	bit[8+i] = temp_bit;
             temp_bit = bit[8+i]; bit[8+i] = bit[16+i];	bit[16+i] = temp_bit;
	    }
    }
    else if(sys_user_para.cfg_para.rgb_format == RGB_FORMAT_BRG)
    {
        /* RGB >> BGR,  BGR >> BRG */
	    for(i=0;i<8;i++){
		     temp_bit = bit[i];   bit[i] = bit[16+i];	bit[16+i] = temp_bit;
             temp_bit = bit[8+i]; bit[8+i] = bit[16+i];	bit[16+i] = temp_bit;
	    }
    }  
    else if(sys_user_para.cfg_para.rgb_format == RGB_FORMAT_BGR)
    {
        /* RGB >> BGR */
	    for(i=0;i<8;i++){
		     temp_bit = bit[i];   bit[i] = bit[16+i];	bit[16+i] = temp_bit;
	    }
    }
}

void set_all_led_same_color(uint32_t rgb_color, uint16_t rgb_length)
{
	for(int i=0; i<rgb_length; i++){
		change_color_to_bit(rgb_color, rgb_pwm_buffer+i*24);
	}
}

void set_rgb_led_same_color(uint32_t rgb_color, uint16_t rgb_length)
{
	for(int i=0; i<rgb_length; i++){
		change_color_to_bit(rgb_color, rgb_pwm_buffer+i*24);
	}
    for(int i=rgb_length; i<RGB_LENGTH_MAX; i++){
		change_color_to_bit(0, rgb_pwm_buffer+i*24);
	}
}

void set_index_led_color(uint32_t index,uint32_t rgb_color)
{
	change_color_to_bit(rgb_color,rgb_pwm_buffer+index*24);
}


void show_running_colorful_led(uint16_t total_color,uint16_t total_led,uint16_t current_index,uint32_t *c_list)
{
	uint16_t i;
	for(i=0;i<total_color;i++){
		if(current_index >= i && current_index < (total_led+i)){
			set_index_led_color(current_index-i,c_list[i]);
		}
	}
}

void reverse_show_running_colorful_led(uint16_t total_color,uint16_t total_led,uint16_t current_index,uint32_t *c_list)
{
	uint16_t i;
	for(i=0;i<total_color;i++){
		if(current_index >= i && current_index < (total_led+i)){
			set_index_led_color(total_led - 1- current_index+i,c_list[i]);
		}
	}
}

#define COLOR_SIZE	7				//七彩灯
//uint32_t color_list[COLOR_SIZE] = {0xfaeee2,0xd6cabe,0xb2a69a,0x8e8276,0x6a5e52,0x463a2e,0x22160a};
uint32_t color_list[COLOR_SIZE] = {0xFAFAFA,0xFAFAFA,0xFAFAFA,0xFAFAFA,0xFAFAFA,0xFAFAFA,0xFAFAFA};
void test_ws2812b(void)
{
	uint16_t j;
	for(j=0;j<RGB_NUM_MAX+COLOR_SIZE;j++){
		set_all_led_same_color(0, 10);
		reverse_show_running_colorful_led(COLOR_SIZE,RGB_NUM_MAX,j,color_list);		
		delay_ms(50);
	}
	delay_ms(1000);
	for(j=0;j<RGB_NUM_MAX+COLOR_SIZE;j++){
		set_all_led_same_color(0, 10);
		show_running_colorful_led(COLOR_SIZE,RGB_NUM_MAX,j,color_list);		
		delay_ms(50);
	}
}
